//
//  LCSettingView.m
//  YiJun
//
//  Created by liangrongchang on 2017/4/17.
//  Copyright © 2017年 Rochang. All rights reserved.
//

#import "LCSettingView.h"
#import "BluetoothCenter.h"
#import "LCInputView.h"
#import "LCAlertPickerView.h"
#import "LCCheckView.h"
#import "LCTableView.h"
@interface LCSettingView ()

// change language label
@property (weak, nonatomic) IBOutlet UILabel *bluetoothName;
@property (weak, nonatomic) IBOutlet UILabel *bluetoothPwd;
@property (weak, nonatomic) IBOutlet UILabel *maxSpeed;
@property (weak, nonatomic) IBOutlet UILabel *carstrong;
@property (weak, nonatomic) IBOutlet UILabel *weak;
@property (weak, nonatomic) IBOutlet UILabel *strong;
@property (weak, nonatomic) IBOutlet UILabel *carturn;
@property (weak, nonatomic) IBOutlet UILabel *weak1;
@property (weak, nonatomic) IBOutlet UILabel *strong1;
@property (weak, nonatomic) IBOutlet UILabel *carDetail;
@property (weak, nonatomic) IBOutlet UILabel *carcheck;
@property (weak, nonatomic) IBOutlet UILabel *runruto;
@property (weak, nonatomic) IBOutlet UILabel *timeing;


@property (weak, nonatomic) IBOutlet UILabel *timeLable;
@property (weak, nonatomic) IBOutlet UISlider *maxSpeedSlider;
@property (weak, nonatomic) IBOutlet UISlider *strongSlider;
@property (weak, nonatomic) IBOutlet UISlider *turnSlider;
@property (weak, nonatomic) IBOutlet UILabel *maxSpeedLabel;
@property (weak, nonatomic) IBOutlet UILabel *strongLabel;
@property (weak, nonatomic) IBOutlet UILabel *turnLabel;

@property (nonatomic, strong) NSMutableArray *timeDataSource;

@end

@implementation LCSettingView

- (void)awakeFromNib {
    [super awakeFromNib];
    // change language label
    self.bluetoothName.text = Language(@"修改设备名称");
    self.bluetoothPwd.text = Language(@"修改设备密码");
    self.maxSpeed.text = Language(@"最大速度设置");
    self.carstrong.text = Language(@"车辆动力强度设置");
    self.weak.text = Language(@"弱");
    self.strong.text = Language(@"强");
    self.carturn.text = Language(@"转向灵敏度设置");
    self.weak1.text = Language(@"弱");
    self.strong1.text = Language(@"强");
    self.carDetail.text = Language(@"车辆详细参数");
    self.carcheck.text = Language(@"车辆系统自动检测");
    self.runruto.text = Language(@"行驶轨迹");
    self.timeing.text = Language(@"定时关机");
    
    
    [self.strongSlider setThumbImage:Image(@"滑动") forState:UIControlStateNormal];
    [self.maxSpeedSlider setThumbImage:Image(@"滑动") forState:UIControlStateNormal];
    [self.turnSlider setThumbImage:Image(@"滑动") forState:UIControlStateNormal];

    self.timeLable.layer.cornerRadius = 15;
    self.timeLable.clipsToBounds = YES;
    NSString *setTime = [BluetoothCenter shareInstance].setTime;
    self.timeLable.text = ![setTime isEqualToString:@"0"] ? setTime : Language(@"未设置");

    self.maxSpeedSlider.minimumValue = 6;
    self.maxSpeedSlider.maximumValue = 18;
    self.maxSpeedLabel.text = [NSString stringWithFormat:@"%@km/h", [BluetoothCenter shareInstance].maxSpeed];
    self.maxSpeedSlider.value = [BluetoothCenter shareInstance].maxSpeed.doubleValue;
    
    self.strongSlider.minimumValue = 50;
    self.strongSlider.maximumValue = 100;
    self.strongLabel.text = [NSString stringWithFormat:@"%@%%", [BluetoothCenter shareInstance].strong];
    self.strongSlider.value = [BluetoothCenter shareInstance].strong.doubleValue;
    
    self.turnSlider.minimumValue = 50;
    self.turnSlider.maximumValue = 100;
    self.turnLabel.text = [NSString stringWithFormat:@"%@%%", [BluetoothCenter shareInstance].turn];
    self.turnSlider.value = [BluetoothCenter shareInstance].turn.doubleValue;
}

- (void)layoutSubviews {
    [super layoutSubviews];
}

+ (LCSettingView *)getLCSettingView {
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].firstObject;
}

- (void)setTimeLabel:(NSString *)time {
    _timeLable.text = time;
}

- (IBAction)nameDidClick:(id)sender {
    [[LCInputView shareInstance] showWithTitle:Language(@"修改设备名称") cancelBlock:^{
        
    } confirmBlock:^(NSString *text) {
        [[BluetoothCenter shareInstance] setupName:text];
    }];
}

- (IBAction)keyDidClick:(id)sender {
    [[LCInputView shareInstance] showWithTitle:Language(@"修改设备密码") cancelBlock:^{
        
    } confirmBlock:^(NSString *text) {
        [[BluetoothCenter shareInstance] setupPassWord:text];
        
    }];
}

- (IBAction)detailDidClick:(id)sender {
    [[LCTableView shareInstance] showWihtTitle:Language(@"设备版本") dataSource:@[Language(@"软件版本:1.0"), Language(@"硬件版本:"), Language(@"其他信息")]];
}

- (IBAction)checkDidClick:(id)sender {
    [[LCCheckView shareInstance] showWithTitle:Language(@"车辆系统自动检测") result:Language(@"故障检测正常")];
}

- (IBAction)rutoDidClick:(id)sender {
    if ([_delegate respondsToSelector:@selector(rutoDidClick:)]) {
        [_delegate rutoDidClick:sender];
    }
}

- (IBAction)timeDidClick:(id)sender {
    __WEAKSELF(self)
    [[LCAlertPickerView shareInstance] showWithTitle:Language(@"定时时间") dataSource:self.timeDataSource complete:^(NSString *text) {
        if ([[BluetoothCenter shareInstance] setupTime:text.intValue]) {
            weakSelf.timeLable.text = text;
        }
    }];
}
- (IBAction)carDidControl:(id)sender {
    if ([_delegate respondsToSelector:@selector(carDidControl:)]) {
        [_delegate carDidControl:sender];
    }
}

- (IBAction)maxSpeedDidChange:(UISlider *)sender {
    self.maxSpeedLabel.text = [NSString stringWithFormat:@"(%zdkm/h)",(int)sender.value];
}

- (IBAction)strongDidChange:(UISlider *)sender {
    self.strongLabel.text = [NSString stringWithFormat:@"(%zd%%)",(int)sender.value];
}

- (IBAction)turnDidChang:(UISlider *)sender {
    self.turnLabel.text = [NSString stringWithFormat:@"(%zd%%)",(int)sender.value];
}

- (IBAction)maxSpeedInside:(UISlider *)sender {
    [[BluetoothCenter shareInstance] setupMaxSpeed:(int)sender.value];
}

- (IBAction)strongInside:(UISlider *)sender {
    [[BluetoothCenter shareInstance] setupStrong:(int)sender.value];
}

- (IBAction)turnInside:(UISlider *)sender {
    [[BluetoothCenter shareInstance] setupTurnStrong:(int)sender.value];
}

#pragma mark - getter
- (NSMutableArray *)timeDataSource {
    if (!_timeDataSource) {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        for (NSInteger i = 0; i < 61; i++) {
            [array addObject:[NSString stringWithFormat:@"%zd", i]];
        }
        _timeDataSource = array;
    }
    return _timeDataSource;
}

@end
